import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';

import '../../tools/KaL_Config.dart';
// import 'package:kerala_lottery/tools/KaL_Config.dart';

class Kal_ResultDetaillistcell extends StatefulWidget {
  Kal_ResultDetaillistcell({super.key, required this.numberMap});
  // List numbers;
  Map numberMap;
  @override
  State<Kal_ResultDetaillistcell> createState() =>
      _Kal_ResultDetaillistcellState();
}

class _Kal_ResultDetaillistcellState extends State<Kal_ResultDetaillistcell> {
  var datas = [];
  var _title = '';
  var _prizeAmount = '';

  //
  double topMargin = 5.0; //item的上下左右间距
  double leftMargin = 10.0; //边距
  double itemHeight = 30; //item高度
  double contentHeight = 0; //内容总高度
  int _crossAxisCount = 5; //横轴个数
  double _childAspectRatio = 16.0; //宽高比

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _prizeAmount = widget.numberMap['prizeAmount'];
    _title = widget.numberMap['title'];
    //数据源
    var numbers = widget.numberMap['number'];
    datas = numbers;

    //取余
    // int remainder = datas.length % _crossAxisCount;
    //行数：有余数需+1
    double countDouble = datas.length / _crossAxisCount;
    int count = countDouble.ceil();
    //总高度= 行数*item高度
    contentHeight = count * itemHeight + (count - 1) * topMargin;
    //如果有余数, 总高度加多一行高度，
    // contentHeight = remainder > 0
    //     ? contentHeight + itemHeight
    //     : contentHeight + (count - 1) * topMargin;
    //根据数据源长度制定横轴个数
    _crossAxisCount = datas.length >= _crossAxisCount ? 5 : 1;
    if (_crossAxisCount < 5) {
      //总高度= 数据源长度*item高度 + (数据源长度-1)*上下间距
      contentHeight =
          datas.length * itemHeight + (datas.length - 1) * topMargin;
    }
    debugPrint('contentHeight---$contentHeight');
  }

  @override
  Widget build(BuildContext context) {
    // 获取屏幕宽度
    double screenWidth = MediaQuery.of(context).size.width;

    //宽高比
    _childAspectRatio = (screenWidth - leftMargin * 4) / itemHeight;
    if (datas.length >= 5) {
      //宽高比=(屏幕宽度-左右边距*4-横轴个数*上下间距)/横轴个数
      _childAspectRatio = ((screenWidth -
                  leftMargin * 4 -
                  ((_crossAxisCount - 1) * topMargin)) /
              _crossAxisCount) /
          itemHeight;
    }

    //
    return Container(
      width: double.infinity,
      margin: EdgeInsets.all(leftMargin),
      // color: HexColor('#eeeeee'),
      decoration: BoxDecoration(
        color: HexColor('#eeeeee'),
        borderRadius: BorderRadius.circular(5),
      ),
      child: Column(
        children: [
          //
          Container(
            alignment: Alignment.center,
            height: 30,
            child: Text(
              '$_title - $_prizeAmount INR',
              style: const TextStyle(
                fontSize: 12,
                fontWeight: FontWeight.w400,
                color: Colors.black,
              ),
            ),
          ),
          Container(
            // margin: EdgeInsets.all(5),
            margin: EdgeInsets.only(
                left: leftMargin, right: leftMargin, bottom: 10),
            height: contentHeight,
            // color: Colors.red,
            child: GridView.builder(
                // padding: const EdgeInsets.all(5),
                physics: const NeverScrollableScrollPhysics(),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: _crossAxisCount, //每行三列
                  childAspectRatio: _childAspectRatio, //显示区域宽高相等
                  crossAxisSpacing: topMargin, // 水平间距
                  mainAxisSpacing: topMargin, // 垂直间距
                ),
                itemCount: datas.length,
                itemBuilder: (context, index) {
                  String number = datas[index];
                  double fontSize = 14;
                  if (number.length >= 8 && datas.length > 1) {
                    fontSize = 10;
                  }

                  return Container(
                    // height: 30,
                    color: Colors.white,
                    alignment: Alignment.center,
                    child: Text(
                      datas[index],
                      style: TextStyle(
                          color: KaL_Config.KaL_MainColor,
                          fontSize: fontSize,
                          fontWeight: FontWeight.w600),
                    ),
                  );
                }),
          ),
        ],
      ),
    );
  }
}
